package com.atgm.mall.gmmailproduct.service.impl;

import com.atgm.common.exception.BizCodeEnume;
import com.atgm.common.exception.GameException;
import com.atgm.common.utils.GmMallUtils;
import com.atgm.mall.gmmailproduct.dao.AttrAttrgroupRelationDao;
import com.atgm.mall.gmmailproduct.entity.AttrAttrgroupRelationEntity;
import com.atgm.mall.gmmailproduct.entity.AttrEntity;
import com.atgm.mall.gmmailproduct.service.AttrService;
import com.atgm.mall.gmmailproduct.vo.SpuItemAttrGroupVo;
import com.atgm.mall.gmmailproduct.vo.resp.AttrGroupWithAttrVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atgm.common.utils.PageUtils;
import com.atgm.common.utils.Query;

import com.atgm.mall.gmmailproduct.dao.AttrGroupDao;
import com.atgm.mall.gmmailproduct.entity.AttrGroupEntity;
import com.atgm.mall.gmmailproduct.service.AttrGroupService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Override
    @Cacheable(cacheNames = "game-attr-group", key = "#root.targetClass")
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        if (GmMallUtils.isNotEmpty(key)) {
            queryWrapper.like("attr_group_name", key);
        }
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    @CacheEvict(cacheNames = "game-attr-group", key = "#root.targetClass")
    public void saveAttrGroup(AttrGroupEntity attrGroup) {
        int count = baseMapper.insert(attrGroup);
        if (count != 1) {
            throw new GameException("添加属性分组失败", BizCodeEnume.ATTR_GROUP.getCode());
        }
    }

    @Override
    @CacheEvict(cacheNames = "game-attr-group", key = "#root.targetClass")
    public void updateAttrGroupById(AttrGroupEntity attrGroup) {
        int count = baseMapper.updateById(attrGroup);
        if (count != 1) {
            throw new GameException("修改属性分组失败", BizCodeEnume.ATTR_GROUP.getCode());
        }
    }

    @Override
    @Transactional
    @CacheEvict(cacheNames = "game-attr-group", key = "#root.targetClass")
    public void deleteAttrGroupById(Long id) {
        int count = baseMapper.deleteById(id);
        if (count != 1) {
            throw new GameException("删除属性分组失败", BizCodeEnume.ATTR_GROUP.getCode());
        }
        //删除关联信息
        QueryWrapper<AttrAttrgroupRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("attr_group_id", id);
        attrAttrgroupRelationDao.delete(queryWrapper);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and((obj) -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });
        }
        if (catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper
            );
            return new PageUtils(page);
        }else {
            queryWrapper.eq("catelog_id", catelogId);
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    queryWrapper
            );
            return new PageUtils(page);
        }
    }

}